// pages/searchList/searchList.js
let util = require('../../utils/util');
const api = require('../../utils/api');
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchKey: "",
    allRecords: "",//所有记录
    recordList: "",//前十条记录搜索列表 
    leftRecord: "",//剩余记录搜索列表 
    hotList: "",//热搜
    addMore: false,//显示更多
    more: false//判断有无更多 

  },
//搜索
  searchKey(e){
    this.setData({
      searchKey: e.detail.value
    })
  },
  toSearch: function (key){
    wx.navigateTo({
      url: '/pages/search/search?keyword=' + key,
    })
  },
  //搜索框输入搜索
  onSearch(e){
    console.log(e);
    let that = this;
    let key = that.data.searchKey || e.detail.value;
    if(key){
      that.toSearch(key);
    }else{
      wx.showToast({
        title: '请输入搜索关键词',
        icon: 'none'
      })
    }
  },
  onHisTouchS: function (e) {
    this.startTime = e.timeStamp;
  },
  onHisTouchE: function (e) {
    this.endTime = e.timeStamp;
  },
  //历史记录搜索
  onHisSearch(e){
    let that = this;
    console.log(e);
    if (that.endTime - that.startTime < 350){
      console.log("tap")
      let curIdx = e.currentTarget.dataset.index;
      that.toSearch(that.data.allRecords[curIdx]);
    }
  },
  // 点击显示更多历史记录
  onMore(){
    this.setData({
      addMore: true,
      more: false
    })
  },
  //长按删除事件
  onDeleteItem(e){
    let that = this;
    wx.showModal({
      title: '删除记录',
      content: '确定删除该历史记录？',
      success: function (res) {
        if (res.confirm) {
          let newList = that.data.allRecords;
          let idx = e.currentTarget.dataset.index;
          newList.splice(idx,1);
          wx.setStorageSync("recordList", newList.reverse())
          that.getData()
        }
      }
    })
  },
  //清除记录的所有缓存数据
  onDelete(){
    let that = this;
    wx.showModal({
      title: '确定删除',
      content: '确定删除全部历史记录？',
      success: function(res){
        if(res.confirm){
          wx.removeStorage({
            key: 'recordList',
            success: function (res) {
              that.getData()
            }
          })
          this.setData({
            more: false,
            addMore: false
          })
        }
      }
    })
  },
  // 获取缓存的搜索记录列表 
  getData:function(){
    let that = this;
    let allRecord = wx.getStorageSync("recordList");
    let len = allRecord.length;
    
    let newList = [];
    if (len > 0) {
      allRecord.forEach(function (item) {
        newList.unshift(item)
      })
      that.setData({ allRecords: newList })
      if (len <= 10) {
        that.setData({
          addMore: false,
          more: false,
          recordList: newList
        })
      } else {
        that.setData({
          more: true,
          addMore: false,
          recordList: newList.slice(0, 10),
          leftRecord: newList.slice(10)
        })
      }
    }else{
      that.setData({
        allRecords: "",
        recordList: "",
        leftRecord: ""
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    let that = this;
    this.getData();
    util.myRequest(api.search,{},
      res=>{
        console.log(1111111111111111,res);
        console.log(res.data.data.hotKeywordList)
        that.setData({
          hotList: res.data.data.hotKeywordList,
        })
      }
    )
  },
})